<!doctype html>
<html lang="en">

<head>
	<title>Home</title>
	{%include file="public/head_common" /%}
	<link rel="stylesheet" href="__STATIC__/assets/css/plug.css">
	<link rel="stylesheet" href="__ROOT__/assets/addons/shopro/libs/element/element.css">
	<link rel="stylesheet" href="__ROOT__/assets/addons/shopro/libs/common.css">
	<style type="text/css">
		input::placeholder{
			color: #bbb;
		}
		.m-ipt{
			-webkit-appearance: none;
		    background-color: #fff;
		    background-image: none;
		    border-radius: 4px;
		    border: 1px solid #dcdfe6;
		    box-sizing: border-box;
		    color: #606266;
		    display: inline-block;
		    font-size: inherit;
		    height: 32px;
		    line-height: 32px;
		    outline: none;
		    padding: 0 10px;
		    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
		    width: 100%;
		}
		.m-ipt:focus {
		    outline: none;
		    border-color: #409eff;
		}
	</style>
</head>

<body>

	<!-- MAIN -->
	<div class="main" id="app">
		<!-- MAIN CONTENT -->
		<div class="main-content">
			<ul class="breadcrumb">
			    <li>仓库管理</li>
			    <li>添加入库</li>
			</ul>
			<hr>
			<div class="container-fluid">
				<div class="row" style="margin-bottom: 10px;">
					<div class="col-sm-4 form-horizontal">
						<div class="form-group">
						    <label class="col-sm-3 control-label">单据编号</label>
						    <div class="col-sm-5">
						    	<input class="m-ipt" v-model="order.number" type="text">
						    </div>
						</div>
						<div class="form-group">  
						    <label class="col-sm-3 control-label">单据类型</label>
						    <div class="col-sm-5">
						    	<select class="form-control" v-model="order.pagetype">
									<option value="0"> 其它入库单 </option>
									<option value="1"> 盘盈单 </option>
								</select>
						    </div>
						</div>
					</div>
					<div class="col-sm-4 form-horizontal">
						<div class="form-group">  
						    <label class="col-sm-3 control-label">单据日期</label>
						    <div class="col-sm-5">
						    	<input class="m-ipt" name="recv_day" type="text" onclick="WdatePicker()">
						    </div>
						</div>
						<div class="form-group">  
						    <label class="col-sm-3 control-label">备注信息</label>
						    <div class="col-sm-5">
						    	<input class="m-ipt" v-model="order.data" type="text">
						    </div>
						</div>
					</div>
				</div>
				<!-- 数据开始-->
				<div class="row" id="result">
					<table class="table table-hover">
						<thead>
							<tr>
								<th>商品</th>
								<th>名称</th>
								<th>所入仓库(批量)</th>
								<th>尺码</th>
								<th>颜色</th>
								<th>数量</th>
								<th>成本价</th>
								<th>备注信息</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(item,i) in items">
								<td style="width: 150px;">
									<a :href="item.goods_pic" title='点击看大图' target="_BLANK">
										<img :src="item.goods_pic" height="50">
									</a>
								</td>
								<td>{{item.goods_name}}</td>
								<td>
									<!-- <input type="text" class="m-ipt" v-model="item.warehouse" size="5">warehouseList -->
									<div class="col-sm-5">
										<select class="form-control" v-model="item.warehouse">
											<option :value="warehouse.id" v-for="warehouse in warehouseList"> {{warehouse.name}} </option>
										</select>
									</div>
								</td>
								<td style="width: 100px;">
									<select class="m-ipt" v-model="item.spec">
										<option value=""> -尺码- </option>
										<option :value="val" v-for="val in item.size_set"> {{ val }} </option>
									</select>
								</td>
								<td><input type="text" class="m-ipt" v-model="item.color" size="5"></td>
								<td><input type="text" class="m-ipt" v-model="item.nums" size="5"></td>
								<td><input type="text" class="m-ipt" v-model="item.origin_price" size="5"></td>
								<td><input type="text" class="m-ipt" v-model="item.data" size="5"></td>
								<td>
									<a href="#" style="color:red" @click="removeItem(i)"><i class="fa fa-trash-o"></i> 删除</a>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				<!-- 数据结束-->	
				<div class="row">
					<a class="btn btn-primary" @click="showGoods()">选择商品</a>
					<button type="button" class="btn btn-success" @click="save()" :disabled="locked">保存</button>
				</div>
			</div>
		</div>
		<!-- END MAIN CONTENT -->
		<vue-modal :visible.sync="showModal" title="选择商品">
			<div class="container">
				<form class="form-inline" style="margin: 10px 0px;">
					<vue-select 
						placeholder="类别"
						v-model="search.cate_name"
						url="{%:url('Goods/getCateListBySort')%}"
						:sorts ="['衣服','下装','鞋','箱包','饰品']"
						style="display: inline-block;width: 100px;">
					</vue-select>
					<vue-select 
						placeholder="品牌"
						v-model="search.brand_name"
						url="{%:url('Brand/getListBySort')%}"
						:sorts ="['AB','CD','EF','GH','IJ','KL','MN','OP','QR','ST','UV','WX','YZ']"
						style="display: inline-block;width: 100px;">
					</vue-select>
					<vue-select 
						placeholder="销售"
						v-model="search.acct_name"
						url="{%:url('Account/getSalers')%}"
						style="display: inline-block;width: 100px;">
					</vue-select>
				    <button type="button" class="btn btn-primary" @click="getGoods()">查询</button>
				</form>
				<table class="table table-bordered table-hover">
					<thead>
						<tr>
							<th>选择</th>
							<th>商品图</th>
							<th>类别</th>
							<th>品牌</th>
							<th>尺码组</th>
							<th>添加者</th>
							<th>添加时间</th>
						</tr>
					</thead>
					<tbody>
						<tr v-for="row in rows">
							<td><input type="checkbox" v-model="sel_goods" :value="row.id"></td>
							<td>
								<a :href="row.goods_pic" title='点击看大图' target="_BLANK">
									<img :src="row.pic_tmb" height="50" class="pre_view">
								</a>
							</td>
							<td>{{ row.cate_name }}</td>
							<td>{{ row.brand }}</td>
							<td>{{ row.size_set }}</td>
							<td>{{ row.acct_name }}</td>
							<td>{{ row.add_time }}</td>
						</tr>
						<tr v-if="rows.length===0">
							<td colspan="8" style="color:red">查询无记录</td>
						</tr>
					</tbody>
				</table>
				<div class="row">
					<div class="col-sm-8">
						<vue-page :total_page='total_page' :cur_page='cur_page' @do-page='getGoods'></vue-page>
					</div>
					<div class="col-sm-4" style="line-height: 75px;">
						<button class="btn btn-success" @click="handleOk">确定</button>
					</div>
				</div>
			</div>
		</vue-modal>
	</div>
	<!-- Javascript -->
	<script src="__STATIC__/assets/vendor/jquery/jquery.min.js"></script>
	<script src="__STATIC__/assets/vendor/vue.js"></script>
	
	<script src="__STATIC__/assets/vendor/base64.js"></script>
	<script src="__STATIC__/components/vue-page.js"></script>
	<script src="__STATIC__/components/vue-select.js"></script>
	<script src="__STATIC__/components/vue-modal.js"></script>
	<script src="__STATIC__/assets/vendor/My97DatePicker/WdatePicker.js"></script>
	<script src="__ROOT__/assets/addons/shopro/libs/element/element.js"></script>
	<script src="__ROOT__/assets/addons/shopro/libs/smartParsePro/pcasCode.js"></script>
	<script src="__ROOT__/assets/addons/shopro/libs/smartParsePro/zipCode.js"></script>
	<script src="__ROOT__/assets/addons/shopro/libs/smartParsePro/address_parse.js"></script>
	<!-- 一键复制 -->
	<script src="__STATIC__/assets/scripts/clipboard.min.js"></script>
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
	<script type="text/javascript">
// 	let smart = smart("陕西省西安市雁塔区丈八沟街道高新四路高新大都荟710061 刘国良 13593464918 211381198512096810")
// 	console.log({smart})
        

					
		// 获取省份
		let dataContainerElem = document.querySelector('.data-box');
		let data = dataContainerElem ? dataContainerElem.dataset : {};
		dataBox = {}; //模板变量容器，`.data-box`类选择器所在的所有`data`属性值集合  
		Object.keys(data).forEach(function (key) {  
			dataBox[key] = data[key];  
			if (isJsonString(data[key])) dataBox[key] = JSON.parse(data[key]); //是json格式的字串才转对象  

		});  
		/**  

		* 判断字串是否属于json字串  

		*/ 
		function isJsonString(str) {  

		let flag = false;  

		if (typeof str != 'string') return flag;  

		try {  

			JSON.parse(str);  
			flag = true;  
		} catch (e) {}  
		return flag;  

		} 
		let provinces = dataBox.provinces;


		var vm = new Vue({
			el: '#app',
			data: {
				konwAddrText:'',
				order: {
					time: '',
					number: '',
					pagetype: 0,
					data: '',
					user: '',
				},
				items: [],
				cities: [],
				showModal: false,
				search: {
					cate_name: '',
					brand_name: '',
					acct_name: ''
				},
				rows: [],
				total_page: 0,
				cur_page: 1,
				sel_goods: [],
				locked: false,
				warehouseList:[]
			},
			components: {
				'vue-modal': vueModal,
				'vue-page': vuePage,
				'vue-select': vueSelect,
			},
			created(){
				this.getWarehouseList();
				this.originNumber();
				console.log(853312111);

				
				let obj = this.getQueryVariable();
				if(('info' in obj)){
					let info = JSON.parse(Base64.decode(obj.info));
					if(info.length>0){
						this.items = info;
						this.order.pagetype = obj.pagetype;
					}
					
				}
			},
			methods:{
				// 获取路由参数
				getQueryVariable() {
					let href = window.location.href
					let query = href.substring(href.indexOf('?')+1);
					let vars = query.split("&");
					let obj = {}
					for (var i = 0; i < vars.length; i++) {
						let pair = vars[i].split("=");
						obj[pair[0]] = pair[1]
					}
					return obj;
				},
				// 初始化编号
				originNumber(){
					let number = 'QTRKD';
					let date = new Date();
					number = number + date.getFullYear().toString() + (date.getMonth() + 1).toString() + date.getDate().toString() + date.getHours().toString() + date.getMinutes().toString() + date.getSeconds().toString();
					this.order.number = number;
				},
				getWarehouseList: function(page=1){
					$.post("{%:url('Warehouse/warehouse_list')%}?page=" + page,data,(rsp) => {
						this.warehouseList = rsp.data.rows;
					},'json')
				},
				getGoods: function(page){
					var _this = this;
					var data = this.search;
					$.post("{%:url('Goods/getList')%}?page=" + page,data,function(rsp){
						_this.rows = rsp.data.rows;
						_this.total_page = rsp.data.total_page;
						_this.cur_page = Number(rsp.data.page);
						_this.sel_goods = [];
					},'json')
				},
				showGoods: function(){
					this.showModal = true;
					this.getGoods(1);
				},
				removeItem(i){
					this.items.splice(i,1);
				},
				handleOk: function(){
					for(var i in this.rows){
						var row = this.rows[i];
						if (this.sel_goods.indexOf(row.id) >= 0) {
							var info = {
								goods: row.id,
								goods_pic: row.goods_pic,
								size_set: row.size_set.split(','),
								warehouse: this.warehouseList.length>0 ? this.warehouseList[this.warehouseList.length-1]['id'] : '',
								spec: '',
								color:'',
								origin_price:'',
								nums: 1,
								data: '',
								attr: -1,
								goods_name: row.goods_name,
								batch: '',
								serial: '',
							};
							this.items.push(info);
						}
					}
					this.showModal = false;
				},
				save: function(){
					this.locked = true;

					var info = this.order;
					info.time = $("input[name='recv_day']").val();
					var data = {
						info: info,
						items: this.items
					};
					var _this = this;
					$.post("{%:url('Warehouse/set_otpurchase')%}",data,function(rsp){
						if (rsp.code == 1) {
							_this.$message.success(rsp.msg);
							let ti = setTimeout(()=>{
								clearTimeout(ti);
								location.href = "{%:url('Warehouse/add_otpurchase')%}"
							},300);
							
						}else{
							_this.$message.error(rsp.msg);
						}
						_this.locked = false;
						
					},'json')
				},
			}
		});
	</script>
</body>

</html>